﻿//***********************************************************************************
// 文件名称：HttpWebHelper
// 功能描述：HttpWeb帮助类
// 数据表：
// 作者：liuxiaohu
// 日期：2017/3/13 9:18:30
// 修改记录：
//***********************************************************************************

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Configuration;

namespace GameClient
{
    /// <summary>
    /// HttpWeb帮助类
    /// </summary>
    public class HttpWebHelper
    {
        #region 变量

        /// <summary>
        /// Web请求对象
        /// </summary>
        private static WebRequest webRequest;

        /// <summary>
        /// Web响应对象
        /// </summary>
        private static WebResponse webResponse;

        /// <summary>
        /// 字节流对象
        /// </summary>
        private static Stream stream;

        /// <summary>
        /// 字节流读取对象
        /// </summary>
        private static StreamReader reader;

        /// <summary>
        /// 错误信息
        /// </summary>
        private static String message;

#if DEBUG
        /// <summary>
        /// 用于调试的url
        /// </summary>
        private static String serverUrl = ConfigurationManager.AppSettings["debugUrl"];
#else
        /// <summary>
        /// 服务器url
        /// </summary>
        private static String serverUrl = ConfigurationManager.AppSettings["serverUrl"];
#endif

        #endregion

        #region 方法

        /// <summary>
        /// 获取玩家积分和排行榜信息
        /// </summary>
        /// <param name="playerId">玩家Id</param>
        /// <returns>积分和排名信息</returns>
        public static RankInfo GetScoreRank(Int32 playerId)
        {
            String getScoreRankUrl = string.Format("{0}HandlerGetRank.ashx?playerId={1}", serverUrl, playerId);

            try
            {
                // 向服务器发送请求并接收服务器的响应
                webRequest = WebRequest.Create(getScoreRankUrl);
                webResponse = webRequest.GetResponse();

                // 读取服务器返回的数据
                stream = webResponse.GetResponseStream();
                reader = new StreamReader(stream, Encoding.UTF8);
                String message = reader.ReadToEnd();

                // 将信息转换为指定类型
                RankInfo info = JsonHelper.JsonToObject<RankInfo>(message);
                return info;
            }
            catch (Exception e)
            {
                throw new Exception(message);
            }
            finally
            {
                // 释放资源
                stream.Close();
                reader.Close();
            }
        }

        /// <summary>
        /// 使用道具
        /// </summary>
        /// <param name="playerId">玩家Id</param>
        /// <param name="itemId">道具Id</param>
        /// <param name="itemNum">道具数量</param>
        /// <returns>使用结果</returns>
        public static String UseItem(Int32 playerId, Int32 itemId, Int32 itemNum)
        {
            String useItemUrl = string.Format("{0}HandlerUseItem.ashx?playerId={1}&itemId={2}&itemNum={3}", serverUrl, playerId, itemId, itemNum);

            try
            {
                // 向服务器发送请求并接收服务器的响应
                webRequest = WebRequest.Create(useItemUrl);
                webResponse = webRequest.GetResponse();

                // 读取服务器返回的数据
                stream = webResponse.GetResponseStream();
                reader = new StreamReader(stream, Encoding.UTF8);
                message = reader.ReadToEnd();
            }
            finally
            {
                // 释放资源
                stream.Close();
                reader.Close();
            }

            return message;
        }

        #endregion
    }
}
